package com.easy.dfc.service;

import com.easy.common.bean.Result;
import com.easy.common.constant.EnumConfig;
import com.easy.common.util.Utility;
import com.easy.dfc.Utils.Util;
import com.easy.dfc.entity.*;
import com.easy.dfc.mapper.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * Created by VULCANTSeries on 2017/5/17.
 */
@Service
public class FactoryHomeService {

    @Resource
    private FactoryHomeMapper factoryHomeMapper;

    @Resource
    private FactorySetMapper factorySetMapper;

    @Resource
    private UserFFFMapper userFFFMapper;

    @Resource
    private DfcFactoryUpgradeMapper factoryUpgradeMapper;

    public Result HomePage(UserPara user) {
        Integer productSize = 6;

        Result rt = new Result();
        FactorySetWithBLOBs factory = factorySetMapper.selectByFactoryId(user.getFactoryId());
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("factoryId",user.getFactoryId());
        map.put("factoryName",factory.getName());
        map.put("factoryImage",factory.getStoreImg());
        map.put("factoryAvatar",factory.getAvatar());

        map.put("factoryType",factory.getType());
        if(factory.getType() == 1)map.put("factoryTypeName","童品汇");
        else if(factory.getType() == 2)map.put("factoryTypeName","潮牌馆");
        else if(factory.getType() == 3)map.put("factoryTypeName","品牌馆");
        else if(factory.getType() == 4)map.put("factoryTypeName","尾品汇");
        else map.put("factoryTypeName","未知");

        int haveFollow = userFFFMapper.getFollowFactoryByUserFactoryId(user.getUserId(),String.valueOf(user.getFactoryId()));

        Integer fansNum = userFFFMapper.getFollowFactoryByFactoryId(String.valueOf(user.getFactoryId()));

        if(haveFollow > 0)map.put("haveFollow",1);
        else map.put("haveFollow",0);
        map.put("fansNum",fansNum);

        //factoryHomeMapper.updateProductSetFactoryId();

        List bannerList = factoryHomeMapper.getFactoryImage(user.getFactoryId(), 1);////顶部轮播
        if(bannerList==null)bannerList = new ArrayList();
        map.put("banner",bannerList);

        List newProductList = factoryHomeMapper.getNewProduct(user.getFactoryId(),0, productSize);////新品
        map.put("newProduct",newProductList);

        List salesRankingList = factoryHomeMapper.getSalesRanking(user.getFactoryId(), productSize);////销量排行
        map.put("salesRanking",salesRankingList);
        rt.setStatus(EnumConfig.S_CODE.OK);
        rt.setData(map);
        return rt;
    }

    public Result AllProductList(ParaAllProduct para) {
        Result rt = new Result();

        para.setPageIndex(para.getPageSize()*para.getPageIndex());
        List allProductList = factoryHomeMapper.AllProductList(para);////销量排行
        rt.setData(allProductList);
        rt.setStatus(EnumConfig.S_CODE.OK);
        return rt;
    }

    public Result NewProductBanner(UserPara para) {
        Result rt = new Result();

        List bannerList = factoryHomeMapper.getFactoryImage(para.getFactoryId(), 2);////顶部轮播
        if(bannerList==null)bannerList = new ArrayList();

        rt.setStatus(EnumConfig.S_CODE.OK);
        rt.setData(bannerList);
        return rt;
    }
    public Result NewProductList(UserPara para) {
        Result rt = new Result();
        para.setPageIndex(para.getPageSize()*para.getPageIndex());
        List newProductList = factoryHomeMapper.getNewProduct(para.getFactoryId(),para.getPageIndex(),para.getPageSize());////新品

        rt.setStatus(EnumConfig.S_CODE.OK);
        rt.setData(newProductList);
        return rt;
    }

    public Result ViewVideo(UserPara para) {
        Result rt = new Result();
        if(para.getType() == 2){
            List videoList = factoryHomeMapper.getLiveVideo(para);////视频直播
            rt.setStatus(EnumConfig.S_CODE.OK);
            rt.setData(videoList);
        }
        else {
            List videoList = factoryHomeMapper.getViewVideo(para);////视频直播
            rt.setStatus(EnumConfig.S_CODE.OK);
            rt.setData(videoList);
        }
        return rt;
    }

    public Result Factory(UserPara user) {
        Integer productSize = 6;

        Result rt = new Result();
        FactorySetWithBLOBs factory = factorySetMapper.selectByFactoryId(user.getFactoryId());
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("factoryId",user.getFactoryId());
        map.put("factoryName",factory.getName());
        map.put("factoryType",factory.getType());
        map.put("factoryImage",factory.getStoreImg());
        map.put("factoryAvatar",factory.getAvatar());

        if(factory.getType() == 1)map.put("factoryTypeName","童品汇");
        else if(factory.getType() == 2)map.put("factoryTypeName","潮牌馆");
        else if(factory.getType() == 3)map.put("factoryTypeName","品牌馆");
        else if(factory.getType() == 4)map.put("factoryTypeName","尾品汇");
        else map.put("factoryTypeName","未知");

        //Followstore followstore = new Followstore();
        //followstore.setUserid(user.getUserId());
        //followstore.setStoreid(String.valueOf(user.getFactoryId()));
        //followstore = followStoreMapper.selectFollow(followstore);
        //
        //
        //List<FollowStoreList> followList = followStoreMapper.selectFollowByFactoryIdList(String.valueOf(user.getFactoryId()));
        //
        //if(followstore == null)map.put("haveFollow",1);
        //else map.put("haveFollow",0);
        //map.put("fansNum",followList.size());

        rt.setStatus(EnumConfig.S_CODE.OK);
        rt.setData(map);
        return rt;
    }

    public Result Upgrade(ParaFactoryUpgrade para,List imageList, String url) {
        Result rt = new Result();
        FactorySetWithBLOBs factory = factorySetMapper.selectByFactoryId(para.getFactoryId());
        if(factory.getType() == para.getUpgradeId()){
            rt.setStatus(EnumConfig.S_CODE.FAIL,"原店铺和升级店铺等级一致不能升级");
            return rt;
        }
        if(factory.getType() > para.getUpgradeId()){
            rt.setStatus(EnumConfig.S_CODE.FAIL,"店铺不能降级");
            return rt;
        }

        DfcFactoryUpgrade upgrade = factoryUpgradeMapper.findByFactoryUpgradeId(para.getFactoryId(),para.getUpgradeId());
        if(upgrade == null) {
            upgrade = new DfcFactoryUpgrade();
            Date now = new Date();

            String imageUrl = "";
            if(imageList !=null ){
                for (int i = 0; i < imageList.size(); i++) {
                    String ossUrl = (String) ((Map<String, String>) imageList.get(i)).get("ossUrl");
                    if (Utility.isNullorEmpty(ossUrl)) continue;
                    imageUrl = imageUrl + ":" + ossUrl;
                }
            }
            upgrade.setFactoryId(para.getFactoryId());
            upgrade.setUpgradeId(para.getUpgradeId());
            if (!Utility.isNullorEmpty(imageUrl)) {
                upgrade.setAptitude(imageUrl.substring(1));
            }
            else {
                upgrade.setAptitude(para.getImages());
            }

            upgrade.setCreateDate(now);
            upgrade.setAuditStatus("0");
            upgrade.setEndStatus("0");
            upgrade.setStartDate(now);
            BigDecimal amnt = new BigDecimal(0);

            if(factory.getType()==1 && upgrade.getUpgradeId()==2){
                amnt = factoryUpgradeMapper.getUpgradeAmnt(1);
            }
            else if(factory.getType()==1 && upgrade.getUpgradeId()==3) {
                amnt = factoryUpgradeMapper.getUpgradeAmnt(2);
            }
            else if(factory.getType()==2 && upgrade.getUpgradeId()==3) {
                amnt = factoryUpgradeMapper.getUpgradeAmnt(3);
            }
            upgrade.setAmnt(amnt);
            factoryUpgradeMapper.addUpgrade(upgrade);
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("auditStatus",upgrade.getAuditStatus());
            map.put("message","申请完成，待审核");
            map.put("payAmnt",upgrade.getAmnt());
            //map.put("auditAmnt",upgrade.getAmnt());
            rt.setData(map);
            rt.setStatus(EnumConfig.S_CODE.OK);
        }
        else {
            if(upgrade.getAuditStatus().equals("0")){//申请中
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("auditStatus",upgrade.getAuditStatus());
                map.put("message","申请中，待审核");
                map.put("payAmnt",upgrade.getAmnt());
                rt.setData(map);
                rt.setStatus(EnumConfig.S_CODE.OK);
            }
            else if(upgrade.getAuditStatus().equals("1")){
                if(upgrade.getEndStatus().equals("0")){
                    Map<String,Object> map = new HashMap<String,Object>();
                    map.put("auditStatus",upgrade.getAuditStatus());
                    map.put("message","审核通过，待支付");
                    map.put("payId",upgrade.getId());
                    map.put("payName","upgradePay");
                    map.put("payAmnt",upgrade.getAmnt());
                    map.put("payFactoryId",para.getFactoryId());
                    if(upgrade.getUpgradeId()==2) {
                        map.put("payProductName","升级为潮流馆");
                        map.put("payProductDesc","店铺由童品汇升级为潮流馆");
                    }
                    else if(upgrade.getUpgradeId()==3) {
                        if(factory.getType()==1){
                            map.put("payProductName","升级为品牌馆");
                            map.put("payProductDesc","店铺由童品汇升级为潮流馆");
                        }
                        else {
                            map.put("payProductName","升级为品牌馆");
                            map.put("payProductDesc","店铺由潮流馆升级为品牌馆");
                        }
                    }
                    map.put("payMode","aliPay,weChatPay");
                    rt.setData(map);
                    rt.setStatus(EnumConfig.S_CODE.OK);
                }
                else {
                    Map<String,Object> map = new HashMap<String,Object>();
                    map.put("auditStatus",upgrade.getAuditStatus());
                    map.put("message","支付完成");
                    map.put("amnt",upgrade.getAmnt());//支付金额
                    rt.setData(map);
                    rt.setStatus(EnumConfig.S_CODE.OK);
                }

            }
            else {
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("auditStatus",upgrade.getAuditStatus());
                map.put("message","已失效");
                map.put("amnt",upgrade.getAmnt());//支付金额
                rt.setData(map);
                rt.setStatus(EnumConfig.S_CODE.OK);
            }
        }
        return rt;
    }

    public String aliNotify(AliNotify aliNotify) {
        String tradeStatus = aliNotify.getTrade_status();
        if(tradeStatus.equals("TRADE_SUCCESS") || tradeStatus.equals("TRADE_FINISHED")){//订单支付完成
            String outTradeNo = aliNotify.getOut_trade_no();
            DfcFactoryUpgrade upgrade = factoryUpgradeMapper.findByTradeNo(outTradeNo);
            if(upgrade != null){
                factorySetMapper.updateFactoryType(upgrade.getFactoryId(),upgrade.getUpgradeId());
                factoryUpgradeMapper.updateSucessStatus(upgrade.getId());
            }

        }
        else if(tradeStatus.equals("TRADE_CLOSED")){//订单关闭
            //String outTradeNo = aliNotify.getOut_trade_no();
            //DfcFactoryUpgrade upgrade = factoryUpgradeMapper.findByTradeNo(outTradeNo);
            //if(upgrade != null){
            //    factorySetMapper.updateFactoryType(upgrade.getFactoryId(),upgrade.getUpgradeId());
            //    factoryUpgradeMapper.updateFailStatus(upgrade.getId());
            //}
        }
        else {//其他错误
            //String outTradeNo = aliNotify.getOut_trade_no();
            //DfcFactoryUpgrade upgrade = factoryUpgradeMapper.findByTradeNo(outTradeNo);
            //if(upgrade != null){
            //    factorySetMapper.updateFactoryType(upgrade.getFactoryId(),upgrade.getUpgradeId());
            //    factoryUpgradeMapper.updateFailStatus(upgrade.getId());
            //}
        }
        return "success";
    }

    public Result UpgradePay(ParaPayInfo para,String url) {
        Result rt = new Result();
        Map<String,Object> map = new HashMap<String,Object>();

        Date now = new Date();
        PayProductInfo info = new PayProductInfo();
        info.setTradeNo(Util.getOrderItemNo());
        info.setTotalAmnt(para.getPayAmnt());
        info.setProductName(para.getPayProductName());
        info.setProductDesc(para.getPayProductDesc());
        info.setCreateTime(now);
        info.setExpireTime(new Date(now.getTime()+900000));
        //info.setNotifyUrl(url+"${apiPath}/factory/aliNotify");
        info.setNotifyUrl(url+"aliNotify");

        if(para.getPayMode().equals("aliPay")){
            AliPayInfo aliPay = new AliPayInfo();
            String zBody = aliPay.getAliPayInfo(info);
            map.put("payId",para.getPayId());
            map.put("tradeNo",info.getTradeNo());
            map.put("payName","aliPay");
            map.put("payInfo",zBody);
            factoryUpgradeMapper.updateTradeNo(para.getPayId(),info.getTradeNo());

        }
        if(para.getPayMode().equals("weChatPay")){
            AliPayInfo aliPay = new AliPayInfo();
            WeChatPayInfo weChatPay = new WeChatPayInfo();
            map.put("payId",para.getPayId());
            map.put("tradeNo",info.getTradeNo());
            map.put("payName","weChatPay");
            map.put("payInfo",weChatPay.getWeChatPayInfo(info));
        }
        rt.setData(map);
        rt.setStatus(EnumConfig.S_CODE.OK);
        return rt;
    }

    public Result UpgradeQuery(ParaFactoryUpgrade para) {
        Result rt = new Result();
        FactorySetWithBLOBs factory = factorySetMapper.selectByFactoryId(para.getFactoryId());
        DfcFactoryUpgrade upgrade = factoryUpgradeMapper.findByFactoryId(para.getFactoryId());
        if(upgrade == null) {
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("auditStatus",-1);
            map.put("factoryId",para.getFactoryId());
            map.put("factoryName",factory.getName());
            map.put("factoryImage",factory.getStoreImg());
            map.put("factoryAvatar",factory.getAvatar());

            map.put("factoryType",factory.getType());
            if(factory.getType() == 1)map.put("factoryTypeName","童品汇");
            else if(factory.getType() == 2)map.put("factoryTypeName","潮牌馆");
            else if(factory.getType() == 3)map.put("factoryTypeName","品牌馆");
            else if(factory.getType() == 4)map.put("factoryTypeName","尾品汇");
            else map.put("factoryTypeName","未知");
            rt.setData(map);
            rt.setStatus(EnumConfig.S_CODE.OK);
        }
        else {
            if(upgrade.getAuditStatus().equals("0")){//申请中
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("auditStatus",upgrade.getAuditStatus());
                map.put("factoryId",para.getFactoryId());
                map.put("factoryName",factory.getName());
                map.put("factoryImage",factory.getStoreImg());
                map.put("factoryAvatar",factory.getAvatar());

                map.put("factoryType",factory.getType());
                if(factory.getType() == 1)map.put("factoryTypeName","童品汇");
                else if(factory.getType() == 2)map.put("factoryTypeName","潮牌馆");
                else if(factory.getType() == 3)map.put("factoryTypeName","品牌馆");
                else if(factory.getType() == 4)map.put("factoryTypeName","尾品汇");
                else map.put("factoryTypeName","未知");

                map.put("upgradeId",upgrade.getUpgradeId());
                map.put("message","申请中，待审核");
                map.put("payAmnt",upgrade.getAmnt());
                rt.setData(map);
                rt.setStatus(EnumConfig.S_CODE.OK);
            }
            else if(upgrade.getAuditStatus().equals("1")){
                if(upgrade.getEndStatus().equals("0")){
                    Map<String,Object> map = new HashMap<String,Object>();
                    map.put("auditStatus",upgrade.getAuditStatus());
                    map.put("factoryId",para.getFactoryId());
                    map.put("factoryName",factory.getName());
                    map.put("factoryImage",factory.getStoreImg());
                    map.put("factoryAvatar",factory.getAvatar());

                    map.put("factoryType",factory.getType());
                    if(factory.getType() == 1)map.put("factoryTypeName","童品汇");
                    else if(factory.getType() == 2)map.put("factoryTypeName","潮牌馆");
                    else if(factory.getType() == 3)map.put("factoryTypeName","品牌馆");
                    else if(factory.getType() == 4)map.put("factoryTypeName","尾品汇");
                    else map.put("factoryTypeName","未知");

                    map.put("message","审核通过，待支付");
                    map.put("upgradeId",upgrade.getUpgradeId());
                    map.put("payId",upgrade.getId());
                    map.put("payName","upgradePay");
                    map.put("payAmnt",upgrade.getAmnt());
                    map.put("payFactoryId",para.getFactoryId());
                    if(upgrade.getUpgradeId()==2) {
                        map.put("payProductName","升级为潮流馆");
                        map.put("payProductDesc","店铺由童品汇升级为潮流馆");
                    }
                    else if(upgrade.getUpgradeId()==3) {
                        if(factory.getType()==1){
                            map.put("payProductName","升级为品牌馆");
                            map.put("payProductDesc","店铺由童品汇升级为潮流馆");
                        }
                        else {
                            map.put("payProductName","升级为品牌馆");
                            map.put("payProductDesc","店铺由潮流馆升级为品牌馆");
                        }
                    }
                    map.put("payMode","aliPay,weChatPay");
                    rt.setData(map);
                    rt.setStatus(EnumConfig.S_CODE.OK);
                }
                else {
                    Map<String,Object> map = new HashMap<String,Object>();
                    map.put("factoryId",para.getFactoryId());
                    map.put("factoryName",factory.getName());
                    map.put("factoryImage",factory.getStoreImg());
                    map.put("factoryAvatar",factory.getAvatar());

                    map.put("factoryType",factory.getType());
                    if(factory.getType() == 1)map.put("factoryTypeName","童品汇");
                    else if(factory.getType() == 2)map.put("factoryTypeName","潮牌馆");
                    else if(factory.getType() == 3)map.put("factoryTypeName","品牌馆");
                    else if(factory.getType() == 4)map.put("factoryTypeName","尾品汇");
                    else map.put("factoryTypeName","未知");

                    map.put("auditStatus",upgrade.getAuditStatus());
                    map.put("upgradeId",upgrade.getUpgradeId());
                    map.put("message","支付完成");
                    map.put("amnt",upgrade.getAmnt());//支付金额
                    rt.setData(map);
                    rt.setStatus(EnumConfig.S_CODE.OK);
                }

            }
            else {
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("factoryId",para.getFactoryId());
                map.put("factoryName",factory.getName());
                map.put("factoryImage",factory.getStoreImg());
                map.put("factoryAvatar",factory.getAvatar());

                map.put("factoryType",factory.getType());
                if(factory.getType() == 1)map.put("factoryTypeName","童品汇");
                else if(factory.getType() == 2)map.put("factoryTypeName","潮牌馆");
                else if(factory.getType() == 3)map.put("factoryTypeName","品牌馆");
                else if(factory.getType() == 4)map.put("factoryTypeName","尾品汇");
                else map.put("factoryTypeName","未知");

                map.put("auditStatus",upgrade.getAuditStatus());
                map.put("upgradeId",upgrade.getUpgradeId());
                map.put("message","已失效");
                map.put("amnt",upgrade.getAmnt());//支付金额
                rt.setData(map);
                rt.setStatus(EnumConfig.S_CODE.OK);
            }
        }
        return rt;
    }
}
